<?php
namespace app\admin\controller;
use app\admin\model\Procate as ProcateModel;
use app\admin\model\Product as ProductModel;
use app\admin\controller\Common;
class Product extends Common
{

    public function lst(){
        $products=db('product')->field('a.*,b.catename')->alias('a')->join('bk_procate b','a.cateid=b.id')->order('a.id desc')->paginate(10);
        $this->assign('products',$products);
        return view();
    }

    public function add(){
        if(request()->isPost()){
            $data=input('post.');
            $data['time']=time();
            //print_r($data);
            $validate = \think\Loader::validate('Product');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $product=new ProductModel;
            if($product->save($data)){
                $this->success('添加产品成功',url('lst'));
            }else{
                $this->error('添加产品失败！');
            }
            return;
        }
        $cate=new ProcateModel();
        $cateres=$cate->catetree();
        $this->assign('cateres',$cateres);
         $province=db('tree')->where(array('pid'=>1))->select();
         $this->assign("province",$province);
        return view();
    }

    public function edit(){
        if(request()->isPost()){
            $data=input('post.');
            $validate = \think\Loader::validate('Product');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $product=new ProductModel;
            $save=$product->update($data);
            if($save){
                $this->success('修改产品成功！',url('lst'));
            }else{
                $this->error('修改产品失败！');
            }
            return;
        }
        $cate=new ProcateModel();
        $cateres=$cate->catetree();
        $arts=db('product')->where(array('id'=>input('id')))->find();
        $this->assign(array(
            'cateres'=>$cateres,
            'arts'=>$arts,
            ));
        $cityname=db('tree')->where(array('id'=>$arts['city']))->find();
        $this->assign('cityname',$cityname['name']);
        $townname=db('tree')->where(array('id'=>$arts['town']))->find();
        $this->assign('townname',$townname['name']);
        $province=db('tree')->where(array('pid'=>1))->select();
         $this->assign("province",$province);
        return view();
    }

    public function del(){
        if(ProductModel::destroy(input('id'))){
            $this->success('删除产品成功！',url('lst'));
        }else{
            $this->error('删除产品失败！');
        }
    }


 

     public function getarea(){
          $map['pid']=input("pid");
          $map['type']=input("type");
          $list=db('tree')->where($map)->select();
          echo json_encode($list);
     }

   

	












}
